package com.sky.vo;

import com.sky.utils.StringUtils;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Data
public class CartVo implements Serializable {

    private Long skuId;

    private Integer count;

    private Integer countType;

    private BigDecimal totalAmount;

    /** 商品减免价格 **/
    private BigDecimal discountAmount = BigDecimal.ZERO;

    private List<CartItemVo> carItemVoList;

    public Integer getCount() {
        AtomicInteger initCount = new AtomicInteger(0);
        if (StringUtils.isNotEmpty(carItemVoList)){
            carItemVoList.stream().forEach(cartItemVo -> {
                initCount.addAndGet(cartItemVo.getCount());
            });
        }
        return initCount.get();
    }

    public Integer getCountType() {
        return StringUtils.isNotEmpty(carItemVoList) ? carItemVoList.size() : 0;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal totalAmount = BigDecimal.ZERO;
        if (StringUtils.isNotEmpty(carItemVoList)){
            totalAmount = carItemVoList.stream().filter(cartItemVo -> cartItemVo.getChecked()).map(cartItemVo -> new BigDecimal(cartItemVo.getCount()).multiply(cartItemVo.getPrice())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
        }

        // 减去优惠的价格
        totalAmount = totalAmount.subtract(discountAmount);
        return totalAmount;
    }
}
